package com.lbj.mall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbj.mall.common.entity.common.spu.MemberPrice;
import com.lbj.mall.common.entity.to.SkuReductionTo;
import com.lbj.mall.common.utils.PageUtils;
import com.lbj.mall.common.utils.Query;
import com.lbj.mall.coupon.dao.SkuFullReductionDao;
import com.lbj.mall.coupon.entity.MemberPriceEntity;
import com.lbj.mall.coupon.entity.SkuFullReductionEntity;
import com.lbj.mall.coupon.entity.SkuLadderEntity;
import com.lbj.mall.coupon.service.MemberPriceService;
import com.lbj.mall.coupon.service.SkuFullReductionService;
import com.lbj.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存sku的优惠满减等信息：
     */
    @Override
    public void saveSkuReductionInfo(SkuReductionTo skuReductionTo) {
        // mall_sms:sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        if (skuReductionTo.getFullCount() > 0) {
            BeanUtils.copyProperties(skuReductionTo, skuLadderEntity);
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        // sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        if (new BigDecimal(0).compareTo(skuReductionTo.getFullPrice()) < 0) {
            BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
            this.save(skuFullReductionEntity);
        }

        // sms_member_price
        List<MemberPrice> memberPriceList = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collectList = memberPriceList.stream()
                .filter(item -> new BigDecimal(0).compareTo(item.getPrice()) < 0)
                .map(item -> {
                    MemberPriceEntity entity = new MemberPriceEntity();
                    entity.setSkuId(skuReductionTo.getSkuId());
                    entity.setMemberLevelId(item.getId());
                    entity.setMemberLevelName(item.getName());
                    entity.setMemberPrice(item.getPrice());
                    entity.setAddOther(1);
                    return entity;
                }).collect(Collectors.toList());
        memberPriceService.saveBatch(collectList);
    }
}


















